package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.pojo.PaperInfo;
import com.hqyj.dao.PaperInfoDao;
import com.hqyj.pojo.PaperQuestion;
import com.hqyj.pojo.PaperQuestionRel;
import com.hqyj.pojo.R;
import com.hqyj.service.PaperInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.service.PaperQuestionRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-10-31 01:49:30
 */
@Service
public class PaperInfoServiceImpl extends ServiceImpl<PaperInfoDao, PaperInfo> implements PaperInfoService {
    @Resource
    PaperInfoDao paperInfoDao;
    //注入中间表的servce(因为：它完成了批量的增删改方法)
    @Autowired
    PaperQuestionRelService paperQuestionRelService;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Transactional//加上事务注解
    @Override
    public R createPaper(PaperInfo paperInfo) {
        //验证试卷名称重名
        QueryWrapper<PaperInfo> queryWrapperW = new QueryWrapper<>();
        //定义根据试卷名称查询条件
        queryWrapperW.eq("paper_name",paperInfo.getPaperName());
        //查询
        PaperInfo p =paperInfoDao.selectOne(queryWrapperW);

        if(p!=null){
            return R.error("试卷名称不能重名");
        }

        //试卷的新增
        int num = paperInfoDao.insert(paperInfo);
        if(num>0){

           //获取选中的试题ID
           String[] quIds = paperInfo.getQuIds().split(",");
            //构建批量插入的数据类型
            List<PaperQuestionRel> list = new LinkedList<>();
            for(String s:quIds){
                PaperQuestionRel pqr = new PaperQuestionRel();
                pqr.setPqId(s);
                pqr.setPaperId(paperInfo.getPaperId());
                list.add(pqr);
            }
            //中间表的批量新增
            boolean is = paperQuestionRelService.saveBatch(list);
            if(is){
                return R.ok();
            }
        }

        return R.error();
    }

    @Override
    public R paperShow(String paperName) {
        PaperInfo p = paperInfoDao.paperShow(paperName);
        if(p!=null){
            return R.ok(p);
        }
        return R.error();
    }

    @Override
    public R queryPaper(String paperName) {
        String key = "exam:"+paperName;
        //判断数据是否有缓存
        Object obj = redisTemplate.opsForValue().get(key);
        PaperInfo p =null;
        if(obj==null){
            //查询数据库
             p = paperInfoDao.paperShow(paperName);
            //存入缓存2小时
            redisTemplate.opsForValue().set(key,p,120, TimeUnit.MINUTES);
        }else {
            //查询缓存
            p=(PaperInfo)obj;
        }
        return R.ok(p);
    }

    @Override
    public R countPaper(String paperName, String s, String m, String j) {
        //获取试卷的正确答案,从缓存中获取
        String key="exam:"+paperName;
        PaperInfo p =(PaperInfo)redisTemplate.opsForValue().get(key);
        //答案分类，分别取出单选，多选，判断的答案
        List<PaperQuestion> sList = new LinkedList<>();
        List<PaperQuestion> mList = new LinkedList<>();
        List<PaperQuestion> jList = new LinkedList<>();

        //遍历试卷答案，分别装在三个集合中，保持答案顺序和考生选中答案顺序一致
        for(PaperQuestion pq:p.getList()){
            if(pq.getQuType().equals("单选题")){
                sList.add(pq);
            }else if(pq.getQuType().equals("多选题")){
                mList.add(pq);
            }else{
                jList.add(pq);
            }
        }

        //取出考试的答案
        String ss[] = s.split(",");
        String ms[] = m.split(",");
        String js[] = j.split(",");

        //计算考生的单选题成绩
        int score=0;
        for(int i=0;i<ss.length;i++){
            if(ss[i].equals(sList.get(i).getQuAnswer())){
                score+=sList.get(i).getQuScore();
            }
        }
        //计算考生的多选题成绩
        for(int i=0;i<ms.length;i++){
            if(ms[i].equals(mList.get(i).getQuAnswer())){
                score+=mList.get(i).getQuScore();
            }
        }
        //计算考生的判断题成绩
        for(int i=0;i<js.length;i++){
            if(js[i].equals(jList.get(i).getQuAnswer())){
                score+=jList.get(i).getQuScore();
            }
        }

        return R.ok(score);
    }
}
